package com.hzj.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzj.pojo.*;
import com.hzj.service.DepartmentService;
import com.hzj.service.HospitalInfoService;
import com.hzj.service.HospitalSetService;
import com.hzj.service.ScheduleService;
import com.hzj.utils.Result;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
//import springfox.documentation.annotations.Cacheable;
import org.springframework.cache.annotation.Cacheable;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/api/hosp/hospital")
public class HospitalApiController {
    @Autowired
    HospitalInfoService his;
    @Autowired
    DepartmentService ds;
    @Autowired
    ScheduleService ss;
    @Autowired
    HospitalSetService hss;

    @PostMapping("/{page}/{limit}")
    Result getPageList(@PathVariable Integer page, @PathVariable Integer limit, @RequestParam(required = false) String hostype, @RequestParam(required = false) String districtCode) {
        int hostype1 = 0;
        int districtCode1 = 0;

        //  医院等级
        if (!StringUtils.isEmpty(hostype)) {
            hostype1 = Integer.valueOf(hostype);
        }
        //  医院所在市区(没有根据有省份分类，后面可以自己加)
        if (!StringUtils.isEmpty(districtCode)) {
            districtCode1 = Integer.valueOf(districtCode);
        }


        return Result.ok(his.getPageListByHostype(new Page(page, limit), hostype1, districtCode1));

    }

    @PostMapping("/findByHosname/{hosname}")
    Result findByHosname(@PathVariable String hosname) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("hosname", hosname);
        List list = his.list(wrapper);

        return Result.ok(list);
    }

    @PostMapping("/{hoscode}")
    HospitalInfo hospitalByHoscode(@PathVariable String hoscode) {

        HospitalInfo hospitalInfo = his.checkByHoscode(hoscode);

        return hospitalInfo;

    }

    //查询所以科室
    @PostMapping("/department/{hoscode}")
    @Cacheable(cacheNames = "departmentByHoscode", key = "#hoscode")
    public List<Map<String, Object>> findDepartment(@PathVariable String hoscode) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("hoscode", hoscode);
        wrapper.groupBy("bigname");
        List<Department> list = ds.list(wrapper);
        List<Map<String, Object>> list1 = new ArrayList();
        //查询科室与科室子类
        for (Department department : list) {
            Map<String, Object> map = new HashMap();
            map.put("id", department.getId());
            map.put("depcode", department.getDepcode());
            map.put("depname", department.getBigname());
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("bigname", department.getBigname());
            wrapper1.eq("hoscode", department.getHoscode());
            List list2 = ds.list(wrapper1);
            map.put("children", list2);
            list1.add(map);
        }


        return list1;
    }

    @PostMapping("/auth/findScheduleList/{hoscode}/{depcode}/{workDate}")
    Result findScheduleList(@PathVariable String hoscode, @PathVariable Integer depcode, @PathVariable String workDate) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("hoscode", hoscode);
        wrapper.eq("depcode", depcode);
        wrapper.eq("work_date", workDate);
        wrapper.ne("docname", "");
        List list = ss.list(wrapper);
        return Result.ok(list);
    }

    @PostMapping("/auth/getBookingScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public Result getBookingScheduleRule(@PathVariable Integer page, @PathVariable Integer limit, @PathVariable String hoscode, @PathVariable Integer depcode) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("hoscode", hoscode);
        wrapper.eq("depcode", depcode);
        wrapper.eq("docname", "");
        wrapper.orderByAsc("work_date");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        wrapper.ge("work_date", sdf.format(new Date()));
//        wrapper.groupBy("work_date");
        //根据上面条件分组分页数据，根据时间分组
        Page page1 = ss.page(new Page(page, limit), wrapper);
        System.out.println("++++++++++" + page1.getRecords());
        Map<String, Object> map = new HashMap();
        map.put("bookingScheduleList", page1);

        //查询医院数据
        HospitalInfo one = his.checkByHoscode(hoscode);


        map.put("baseMap", one);

        return Result.ok(map);
    }

    @PostMapping("/auth/getSchedule/{id}")
    Schedule getSchedule(@PathVariable Integer id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", id);
        Schedule one = ss.getOne(wrapper);
        return one;
    }

    @PostMapping("/auth/getDepartmentByCode/{depcode}")
    Department getDepartmentByCode(@PathVariable String depcode) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("depcode", depcode);
        Department one = ds.getOne(wrapper);


        return one;
    }

    @PostMapping("/auth/getHospitalSetByHoscode/{hoscode}")
    HospitalSet getHospitalSetByHoscode(@PathVariable String hoscode) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("hoscode", hoscode);
        return hss.getOne(wrapper);
    }

    @PostMapping("/auth/updateScheduleById")
    Boolean updateScheduleById(@RequestBody Schedule schedule) {
        return ss.updateById(schedule);
    }

    /**
     * rabbit
     *
     * @param schedule
     */
    @RabbitListener(queues = {"Fanout.queue.updateScheduleByIdQueue"})
    void updateScheduleByIdRabbtiMq(String schedule) {
        System.out.println("~~~~~~执行rabbit：updateScheduleByIdQueue");
        Schedule schedule1 = JSONObject.parseObject(schedule, Schedule.class);
        ss.updateById(schedule1);
    }

    @PostMapping("/auth/getScheduleByAddHosScheduleId/{hosScheduleId}")
    boolean getScheduleByAddHosScheduleId(@PathVariable String hosScheduleId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("hosScheduleId", hosScheduleId);
        Schedule schedule = ss.getOne(wrapper);
        System.out.println("9-0===>" + schedule);
        schedule.setAvailableNumber(schedule.getAvailableNumber() + 1);
        System.out.println("0-9===>" + schedule);
        return ss.updateById(schedule);
    }


}
